/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.stylefeng.guns.modular.grape.service;

import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import static cn.stylefeng.guns.core.util.UuidUtil.get32UUID;
import cn.stylefeng.guns.modular.grape.entity.Enterprise;
import static cn.stylefeng.guns.modular.grape.enums.StatusEnum.DELETED;
import cn.stylefeng.guns.modular.grape.mapper.EnterpriseMapper;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import static java.util.Calendar.getInstance;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author wmao
 */
@Service
public class EnterpriseService extends ServiceImpl<EnterpriseMapper, Enterprise> {

    @Resource
    private EnterpriseMapper enterpriseMapper;
    
    @Transactional(rollbackFor = Exception.class)
    public void addEnt(Enterprise ent) {
        if (ToolUtil.isOneEmpty(ent, ent.getCopCode(), ent.getCopName(), ent.getDxpId(), ent.getDxpMode())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        Date currentTime = getInstance().getTime();
        ent.setId(get32UUID());
        ent.setCreateTime(currentTime);
        ent.setUpdateTime(currentTime);
        this.save(ent);
    }
    
    @Transactional(rollbackFor = Exception.class)
    public void editEnt(Enterprise ent) {
        if (ToolUtil.isOneEmpty(ent, ent.getCopCode(), ent.getCopName(), ent.getDxpId(), ent.getDxpMode())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        ent.setUpdateTime(getInstance().getTime());
        this.updateById(ent);
    }
    
    @Transactional
    public void deleteEnt(String entId) {
        Enterprise ent = enterpriseMapper.selectById(entId);
        ent.setStatus(DELETED.getIdx());
        ent.setUpdateTime(getInstance().getTime());
        this.updateById(ent);
    }
    
    public Enterprise getOnlyOne(){
        return enterpriseMapper.getOnlyOne();
    }
}

